package com.hailiao.beidou.message.data.db

import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.hailiao.beidou.message.data.startup.ctx

/**
 * 数据库操作
 * @Author d10ng
 * @Date 2024/3/1 15:31
 */
object DB {

    val instant by lazy {
        Room.databaseBuilder(ctx, ADB::class.java, "app.db")
            .addMigrations(MIGRATION_1_2)
            .build()
    }

    private val MIGRATION_1_2: Migration = object : Migration(1, 2) {
        override fun migrate(db: SupportSQLiteDatabase) {
            // Since index creation doesn't affect the data, we can just add the index.
            db.execSQL("CREATE INDEX IF NOT EXISTS index_message_table_number_isSendOut_status_isLocation ON message_table(number, isSendOut, status, isLocation)")
            db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS index_contact_table_number ON contact_table(number)")
        }
    }
}